package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.CouponServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: AdminCouponServlet
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/5/30 16:15
 * @Version: V1.0
 */
@WebServlet("/admin/coupon/*")
public class AdminCouponServlet extends WdBaseServlet {

    CouponService couponService = new CouponServiceImpl();

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String typeStr = request.getParameter("type");
        Short type = StringUtil.isNotEmpty(typeStr) ? Short.parseShort(typeStr) : null;
        Short status = StringUtil.isNotEmpty(request.getParameter("status")) ? Short.parseShort(request.getParameter("status")) : null;
        Integer page = StringUtil.isNotEmpty(request.getParameter("page")) ? Integer.parseInt(request.getParameter("page")) : null;
        Integer limit = StringUtil.isNotEmpty(request.getParameter("limit")) ? Integer.parseInt(request.getParameter("limit")) : null;
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        CommonData<MarketCoupon> data = couponService.querySelective(name, type, status, page, limit, sort, order);
        ResponseUtil.responseJson(response, BaseRespVo.ok(data));
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        String readLine = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(readLine, MarketCoupon.class);

        BaseRespVo baseRespVo = couponService.create(marketCoupon);

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        String readLine = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(readLine, MarketCoupon.class);

        BaseRespVo baseRespVo = couponService.update(marketCoupon);

        return baseRespVo;

    }

    //优惠券详情
    public BaseRespVo listuser(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Short status = StringUtil.isNotEmpty(request.getParameter("status")) ? Short.parseShort(request.getParameter("status")) : null;
        Integer page = StringUtil.isNotEmpty(request.getParameter("page")) ? Integer.parseInt(request.getParameter("page")) : null;
        Integer limit = StringUtil.isNotEmpty(request.getParameter("limit")) ? Integer.parseInt(request.getParameter("limit")) : null;
        Integer couponId = StringUtil.isNotEmpty(request.getParameter("couponId")) ? Integer.parseInt(request.getParameter("couponId")) : null;
        Integer userId = StringUtil.isNotEmpty(request.getParameter("userId")) ? Integer.parseInt(request.getParameter("userId")) : null;

        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        BaseRespVo baseRespVo = couponService.listuser(page, limit, couponId, userId, status, sort, order);
        return baseRespVo;
    }

    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {

        String s = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(s, MarketCoupon.class);

        BaseRespVo baseRespVo = couponService.delete(marketCoupon);

        return baseRespVo;

    }

    public BaseRespVo read(HttpServletRequest request,HttpServletResponse response){

        Integer id = Integer.parseInt(request.getParameter("id"));
        BaseRespVo baseRespVo = couponService.read(id);

        return baseRespVo;


    }
}
